﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Innvoice.Logica.GestorDB;
using Innvoice.Logica.GestorObject.Articulo;
using System.ComponentModel;

namespace Innvoice.Ws.Articulos.Articulos
{
    /// <summary>
    /// Lógica de interacción para ModificarCategoria.xaml
    /// </summary>
    public partial class ModificarArticulo : Window
    {

        #region Property

        private Articulo _articulo;
        private ArticuloDB articuloDb = new ArticuloDB();

        #endregion

        #region Contructor

        public ModificarArticulo()
        {
            InitializeComponent();
        }

        public ModificarArticulo(Articulo articulo)
        {
            InitializeComponent();
            _articulo = articulo;
            DataContext = _articulo;
            _articulo.PropertyChanged += new PropertyChangedEventHandler(articulo_PropertyChanged);
            TextDescripcion.Focus();
        }

        #endregion

        #region Windows Methods

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


            List<CategoriaArticulo> categorias = new List<CategoriaArticulo>();

            categorias = articuloDb.ObtenerCategoriasDeArticulosSegunEstado(1);

            categoriaComboBox.Items.Clear();
            categoriaComboBox.ItemsSource = categorias;
            categoriaComboBox.SelectedIndex = _articulo.CodigoCategoriaArticulo - 1;

        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;

            }
        }

        private void articulo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = _articulo.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            _articulo.CodigoCategoriaArticulo = Convert.ToInt16(categoriaComboBox.SelectedValue);
            articuloDb.UpdateArticulo(_articulo, true);
            this.Close();
        }

        #endregion
    }
}
